package com.ojoin.trade.t0loan_core.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.ojoin.trade.common.constdef.IConstDef;
import com.ojoin.trade.common.utils.UUIDUtils;

/**
 * token生成器
 * 
 * @author guoting
 */
public class TokenUtils {

	public static String generateToken() {
		return UUIDUtils.create();
	}
	
	/**
	 * 生成token
	 * 最后两位是checksum
	 * 
	 * @param factoringOperatorId
	 *            保理员id
	 * @param creditorMhtno
	 *            债权人商户号
	 * @param debtorMhtno
	 *            债务人商户号
	 * @param operationType
	 *            转账类型（放贷，收贷）
	 * @return orderId
	 */
	public static String generateToken(long factoringOperatorId,
			String creditorMhtno, String debtorMhtno,
			String transferType) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				"yyyyMMddHHmmss");
		String ret = "T0_" + simpleDateFormat.format(new Date()) + "_"
				+ factoringOperatorId + "_" + getLast10ByMhtno(creditorMhtno) + "_" + getLast10ByMhtno(debtorMhtno) + "_"
				+ transferType;
		int checksum = 0;
		for (int i = 0; i < ret.length(); i++) {
			checksum += ret.charAt(i);
		}
		return ret + "_" + (checksum % 100);
	}

	private static String getLast10ByMhtno(String mhtno) {
		if (mhtno != null && mhtno.length() >= 10) {
			return mhtno.substring(mhtno.length()-10, mhtno.length());
		}
		
		return mhtno;
	}
	
  /**
   * @param args
   */
  public static void main(String[] args) {
    System.out.println(
    		generateToken(112, "mchid01", "mchid67909999", IConstDef.T0TransferType_CREDIT));
  }

}